package com.example.gtsp.hwdemo.util;

import org.dom4j.*;
import java.util.Iterator;

/**
 * dom4j解析xml
 */
public class OjbkDom4j {
    public static void main(String[] args) {
        try {
            parseDom(getXml("response"));
        }catch(DocumentException e){
            e.printStackTrace();
            System.out.println("解析失败。。。");
        }
    }


    public static void parseDom(String xml) throws DocumentException {
        Document doc = DocumentHelper.parseText(xml);
        System.out.println("解析成功");
        Element root = doc.getRootElement();
        Iterator iterator = root.elementIterator("responsehead");
        System.out.println("解析responsehead成功");
        Element element;
        if (iterator.hasNext()) {
            element = (Element) iterator.next();
            String uuid = element.elementText("uuid");
            String timestamp = element.elementText("timestamp");
            System.out.println("解析responsehead成功timestamp="+timestamp);
            if ("".equals(element.elementText("errormessage"))) {
                System.out.println("解析errormessage=KONG,开始解析responsebody");
                iterator = root.elementIterator("responsebody");
                if (iterator.hasNext()) {
                    element = (Element) iterator.next();
                    iterator = element.elementIterator("data");
                    System.out.println("解析responsebodyG,data");
                    if (iterator.hasNext()) {
                        element = (Element) iterator.next();
                        System.out.println("PrpDSolvency");
                        iterator = element.elementIterator("PrpDSolvency");
                        if (iterator.hasNext()) {
                            element = (Element) iterator.next();
                            String revealInfo = element.elementText("revealInfo");
                            System.out.println("revealInfo="+revealInfo);
                        }
                    }
                }
            }
        }
    }

    public static String getXml(String xmlType) {
        String requestXml = "";

        if (xmlType.contains("request")) {
            requestXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<GetSolvencyBydateInterFace>\n" +
                    "<requestbody>\n" +
                    "<systemCode>01010801</systemCode>\n" +
                    "<writeDate>2021-05-06 05:00:00.000 CST</writeDate>\n" +
                    "<REQUEST_TYPE>D204</REQUEST_TYPE>\n" +
                    "</requestbody>\n" +
                    "</GetSolvencyBydateInterFace>";
        } else {
            requestXml = "<ResponseXml>\n" +
                    "<responsehead>\n" +
                    "<uuid></uuid>\n" +
                    "<timestamp>2021-05-06 04:59:58.844 CST</timestamp>\n" +
                    "<request_type></request_type>\n" +
                    "<response_code>1</response_code>\n" +
                    "<server_version></server_version>\n" +
                    "<sender>0209</sender>\n" +
                    "<errormessage></errormessage>\n" +
                    "</responsehead>\n" +
                    "<responsebody>\n" +
                    "<pageSize>0</pageSize>\n" +
                    "<pageNo>0</pageNo>\n" +
                    "<data>\n" +
                    "<PrpDSolvency>\n" +
                    "<validDate class=\"sql-date\">2021-04-30</validDate>\n" +
                    "<comprehensiveRating></comprehensiveRating>\n" +
                    "<operatorCode>11107722</operatorCode>\n" +
                    "<operateTimeForHis class=\"sql-timestamp\">2021-04-25 11:11:32.0</operateTimeForHis>\n" +
                    "<createCode>11107722</createCode>\n" +
                    "<insertTimeForHis class=\"sql-timestamp\">2021-04-25 11:11:32.0</insertTimeForHis>\n" +
                    "<validInd>1</validInd>\n" +
                    "<revealInfo>航服能力230%</revealInfo>\n" +
                    "</PrpDSolvency>\n" +
                    "</data>\n" +
                    "</responsebody>\n" +
                    "</ResponseXml>";
        }
        return requestXml;

    }
}
